<template>
  <div>
    <!-- 渲染角色列表 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="addRole">确定</el-button>
        <el-button size="small" @click="cancelrole">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
// 获取角色列表
import { getRoles } from '@/api/setting'
// 根据id 获取角色信息
import { getUserDetailById } from '@/api/user'
// 更新角色接口
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    userRoleid: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 被选中的数据
      roleList: []// 角色列表数据
    }
  },
  created() {
    this.getRoles()
    this.getUserDetailById()
  },
  methods: {
    // 点击更新用户权限
    async addRole() {
    // 这里需要传入，当前用户的id和当前的选中状态
      const res = await assignRoles({ id: this.userRoleid, roleIds: this.roleIds })
      this.$message.success(res.message)
      this.getRoles()
      this.$emit('close')
    },
    // 获取角色列表
    async getRoles() {
      const res = await getRoles()
      this.roleList = res.data.rows
    },
    // 根据id获取用户信息中的选中项，赋值给复选框
    async getUserDetailById() {
      const res = await getUserDetailById(this.userRoleid)
      this.roleIds = res.data.roleIds
    },
    // 点击取消关闭弹窗
    cancelrole() {
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 30px;
}
</style>
